#!/bin/bash
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/sys_check.sh"
if [ -f /etc/os-release ]; then
   echo "Linux system."
elif [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
   echo "macOS system."
   if [ -f ~/.bash_profile ]; then
      source ~/.bash_profile
    fi
elif current_os_is_windows; then
   echo "Windows system."
else
   echo "Unknown OS"
fi

APP_INSTALL_NAME="liveportrait"
APP_TARGET_DIR_NAME="LivePortrait"

echo "lmdprogress:3"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_conda_pytorch.sh"
source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_run.sh"
source "$LMD_BASE_INSTALL_SCRIPT_DIR/apps/$APP_INSTALL_NAME/env"

export CURRENT_APP_PATH="${LMD_APPS_DIR}/${APP_TARGET_DIR_NAME}"
echo $CURRENT_APP_PATH
mkdir -p "$CURRENT_APP_PATH"

echo "lmdprogress:8"

if [ -d "$CURRENT_APP_PATH" ]; then
    echo "App Dir: "$CURRENT_APP_PATH
else
    echo "Error, App Dir dose not exist, please retry. "$CURRENT_APP_PATH
    exit 1
fi

echo "lmdprogress:12"

export GIT_HOST=${GITHUB_PROXY}"https://github.com"
export GIT_REPO_URL="${GIT_HOST}/KwaiVGI/LivePortrait"
export GIT_REPO_BRANCH="main"
export LOCAL_REPO_PATH="$CURRENT_APP_PATH"
sh "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/git_clone_and_fetch.sh"

echo "lmdprogress:40"

source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/conda_and_pip_settings.sh"


conda --version
echo "lmdprogress:41"
cd "$CURRENT_APP_PATH"
# CONDA_ENV_NAME=$APP_INSTALL_NAME
CONDA_PREFIX="./venv"

if [ -d "$CONDA_PREFIX" ]; then
    echo "found ./venv. remove it. "
    rm -rf "$CONDA_PREFIX"
fi
# conda create -p $CONDA_PREFIX python=3.10 -y
export PYTHON_VER_ENV=3.10
conda_create_env $CONDA_PREFIX python=$PYTHON_VER_ENV -y

echo "lmdprogress:45"

cd "$CURRENT_APP_PATH"

echo "lmdprogress:46"
echo "install torch"


function install_ffmpeg {
  source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_ffmpeg.sh"
}

function check_ffmpeg_and_install_win {
  PROGRAMS=("ffmpeg" "ffprobe")
  missing_programs=0

  for PROGRAM in "${PROGRAMS[@]}"; do
    if ! command -v ${PROGRAM} >/dev/null 2>&1; then
      $PROGRAM -v
      echo "$PROGRAM not found"
      missing_programs=1
    fi
  done

  if [ $missing_programs -eq 1 ]; then
    echo "need install ffmpeg and ffprobe。"
    install_ffmpeg
  else
    echo "ffmpeg and ffprobe exist"
  fi
}

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_mac.sh"
    conda_run_conda_forge_install ffmpeg
elif current_os_is_windows; then
    # conda run -p $CONDA_PREFIX --no-capture-output pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu126
    source "${LMD_BASE_INSTALL_SCRIPT_DIR}/global/install_torch_multi_method.sh"
    check_ffmpeg_and_install_win
fi

echo "lmdprogress:50"

# Install pydantic 2.10.6, because the newest version is not compatiable.
conda_run_pip_install huggingface_hub xet pydantic==2.10.6

echo "lmdprogress:60"
# This project is special. Use conda to install cudatoolkit and cudnn.
conda run -p $CONDA_PREFIX --no-capture-output conda install cudatoolkit cudnn -c pytorch -y

echo "lmdprogress:70"

if [ -f /System/Library/CoreServices/SystemVersion.plist ]; then
    conda_run_pip_install -r requirements_macOS.txt
elif current_os_is_windows; then
    conda_run_pip_install -r requirements.txt
fi

echo "lmdprogress:80"

if [ -z "$HF_ENDPOINT" ] && [ -n "$HF_MIRROR" ]; then
    export HF_ENDPOINT="$HF_MIRROR"
fi

echo "start download models"

# conda run -p $CONDA_PREFIX --no-capture-output huggingface-cli download KwaiVGI/LivePortrait --local-dir pretrained_weights --exclude "*.git*" "README.md" "docs"
conda_run_hf_download KwaiVGI/LivePortrait --local-dir pretrained_weights --exclude "*.git*" "README.md" "docs"

echo "lmdprogress:100"
